/**
 * 说明：
 * 创建人：pluto
 * 创建时间：2018/8/3
 */
var AdCtrl = function ($scope, AdService, $uibModal) {
    $scope.result = {};
    var param = {pageNum: 1, pageSize: 10};


    var loadData = function (param) {
        AdService.listOfPage(param).then(function (res) {
            $scope.result = res;
        });
    };


    $scope.onPageChange = function () {
        param.pageNum = $scope.result.page.currentNo;
        loadData(param);
    };
    loadData(param);
    $scope.toEdit = function (data) {
        var modalInstance = $uibModal.open({
            templateUrl: 'pages/system/view/adedit.html',
            controller: 'AdEditCtrl',
            backdrop: "edit",
            size: 'md',
            resolve: {
                data: function () {
                    return data;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            param.pageNum = 1;
            loadData(param);
        }, function (aaa) {
            // $log.info('Modal cancel at: ' + aaa + "," + new Date());
        });
    };
    $scope.doUpdState = function (data) {
        var p = {id: data.id};
        var msg;
        if(data.state == 0){
            msg = '启用';
            p.state = 1;
        }
        if(data.state == 1){
            msg = '停用';
            p.state = 0;
        }
        layer.confirm('是否'+msg+'广告', {btn: ['确定', '取消'], title: "提示"}, function () {
            if(data.state == 0){
                AdService.enable(data.id).then(function (res) {
                    loadData(param);
                    layer.msg(msg+'成功', {icon: 1});
                }).catch(function (err) {
                    console.log(err);
                    layer.msg(err.message, {icon: 2});
                })
            }else{
                AdService.edit(p).then(function (res) {
                    loadData(param);
                    layer.msg(msg+'成功', {icon: 1});
                }).catch(function (err) {
                    console.log(err);
                    layer.msg(err.message, {icon: 2});
                })
            }
        });

    };
    $scope.doDelete = function (id) {
        layer.confirm("确定删除？", {btn: ['确定', '取消'], title: "提示"}, function () {
            AdService.delete(id).then(function (res) {
                param.pageNum = 1;
                loadData(param);
                layer.msg('删除成功', {icon: 1});
            })
        })
    }

};
var AdEditCtrl = function ($scope, $uibModalInstance, data, AdService) {
    $scope.uploadUrl = UPLOAD_URL+'v0/file/upload';
    $scope.ad = angular.copy(data);
    $scope.files = [];
    $scope.doEdit = function () {
        $scope.ad.url = $scope.files.join('');
        AdService.edit($scope.ad).then(function (res) {
            $uibModalInstance.close();
        }, function (error) {
            console.log(error);
        })
    };
    $scope.close = function () {
        $uibModalInstance.dismiss();
    }

};
angular.module('app.system.ad.controller',[])
    .controller("AdCtrl", AdCtrl)
    .controller("AdEditCtrl", AdEditCtrl);

